package plugins

import (
	"github.com/quarkcloudio/quark-go/v2/pkg/builder"
	"github.com/quarkcloudio/quark-go/v2/pkg/dal/db"
	"github.com/wsl/hi-go/model"
	imageUploadManager "github.com/wsl/hi-go/plugins/upload"
	"gorm.io/driver/mysql"
	"gorm.io/gorm"
)

type DB struct{}

func (p *DB) InitDB() *builder.Engine {
	dsn := "root:root1234" + "@tcp(" + "127.0.0.1" + ":" + "3306" + ")/" + "go_demo" + "?charset=" + "utf8" + "&parseTime=True&loc=Local"

	b := builder.New(&builder.Config{
		AppKey: "212121",
		DBConfig: &builder.DBConfig{
			Dialector: mysql.Open(dsn),
			Opts:      &gorm.Config{},
		},
		Providers: []interface{}{&imageUploadManager.Image{}},
	})

	// 迁移数据
	db.Client.AutoMigrate(
		&model.User{},
		&model.Student{},
		&model.Class{},
	)
	return b
}
